package com.wx.tnsenterprise.controller;

import com.github.pagehelper.PageInfo;
import com.wx.common.api.CommonResult;
import com.wx.tnsenterprise.model.dto.PageDto;
import com.wx.tnsenterprise.model.po.EnterprisePo;
import com.wx.tnsenterprise.model.po.HrPo;
import com.wx.tnsenterprise.model.dto.HrDto;
import com.wx.tnsenterprise.model.dto.HrRegisterAndLoginDto;
import com.wx.tnsenterprise.service.HrService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author 林建权
 * @version 1.0
 * @date 2020/7/28 17:53
 */
//HR信息接口
@CrossOrigin
@RestController
@RequestMapping("/hr")
@Api(tags = "HR信息接口")
public class HrController {

    @Autowired
    private HrService hrService;

    //前端需求

    //获取验证码
    @PostMapping("/getCaptcha")
    @ApiOperation(value = "获取验证码",notes = "获取验证码")
    public CommonResult getCaptcha(@RequestBody HrRegisterAndLoginDto hrRegisterAndLoginDto){
        try{
            hrService.getCaptcha(hrRegisterAndLoginDto);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("获取验证码失败！");
        }
        return CommonResult.success("已发送验证码！");
    }

    //登录或注册
    @PostMapping("/registerOrLogin")
    @ApiOperation(value = "hr登录注册",notes = "hr登录注册")
    public CommonResult register(@RequestBody HrRegisterAndLoginDto hrRegisterAndLoginDto){
        Integer result = null;
        try{
            result = hrService.registerOrLogin(hrRegisterAndLoginDto);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("hr注册或登录失败！");
        }
        if (result>0){
            return CommonResult.success(result);
        }else {
            return CommonResult.failed("登录失败！");
        }

    }

    //hr修改信息
    @PostMapping("/updateByHrDto")
    @ApiOperation(value = "创建BOSS名片", notes = "修改HR数据")
    public CommonResult updateByHrDto(HrDto hrDto,@RequestParam(value = "hrPic") MultipartFile hrPic) {
        int result = 0;
        try{
            result = hrService.updateByHrDto(hrDto,hrPic);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("修改失败！");
        }
        return CommonResult.success("修改"+result+"条数据成功！");
    }

    //获取全部HR信息
    @GetMapping("/all")
    @ApiOperation(value = "获取全部HR信息",notes = "获取全部HR信息")
    public CommonResult<List<HrPo>> findAll(){
        List<HrPo> list = null;
        try{
            list = hrService.findAll();
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("查询全部HR数据失败");
        }
        return CommonResult.success(list);
    }

    //获取全部HR信息（分页）
    @PostMapping("/allPageInfo")
    @ApiOperation(value = "获取全部HR信息（分页）",notes = "获取全部HR信息（分页）")
    public CommonResult<PageInfo<HrPo>> findAllPageInfo(@RequestBody PageDto pageDto){
        PageInfo<HrPo> list = null;
        try{
            list = hrService.findAllPageInfo(pageDto);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("查询全部HR数据失败");
        }
        return CommonResult.success(list);
    }

    //获取单个HR信息
    @GetMapping("/findById")
    @ApiOperation(value = "获取单个HR信息",notes = "获取单个HR信息")
    public CommonResult<HrPo> findById(@RequestParam Integer id){
        HrPo result = null;
        try{
            result = hrService.findById(id);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("获取单个HR信息失败");
        }
        return CommonResult.success(result);
    }

    //测试Service接口
    @PostMapping("/insert")
    @ApiOperation(value = "添加HR", notes = "添加HR")
    public CommonResult insert(@RequestBody HrPo hrPo) {
        int result = 0;
        try{
            result = hrService.insert(hrPo);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("添加失败！");
        }
        return CommonResult.success("添加"+result+"条数据成功！");
    }


    @PostMapping("/update")
    @ApiOperation(value = "修改HR数据", notes = "修改HR数据")
    public CommonResult update(@RequestBody HrPo hrPo) {
        int result = 0;
        try{
            result = hrService.update(hrPo);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed("修改失败！");
        }
        return CommonResult.success("修改"+result+"条数据成功！");
    }


    @GetMapping("/delete")
    @ApiOperation(value = "删除HR数据", notes = "删除HR数据")
    public CommonResult delete(@RequestParam Integer id) {
        int result = 0;
        try{
            result = hrService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.success("删除失败！");
        }
        return CommonResult.success("删除"+result+"条数据成功！");
    }
}
